<template>
	<view class="label">
		<view class="label-box">
			<view class="label-header">
				<view class="label-title">
					我的标签
				</view>
				<view class="label-edit" @click="editLabel">
					{{is_edit?"完成":"编辑"}}
				</view>
			</view>
			<uni-load-more v-if="loading" status="loading" iconType="snow"></uni-load-more>
			<view class="label-content" v-if="!loading">
				<view class="label-content__item" v-for="(item,index) in labelList" :key="item.id">
					{{item.name}}
					<uni-icons type="clear" size="20" color="red" class="icons-close" v-show="is_edit" @click="del(index)"></uni-icons>
				</view>
				<view class="no-data" v-if="labelList.length ===0 && !loading">
					当前没有数据
				</view>
			</view>
		</view>
		
		<view class="label-box">
			<view class="label-header">
				<view class="label-title">
					标签推荐
				</view>
			</view>
			<uni-load-more v-if="loading" status="loading" iconType="snow"></uni-load-more>
			<view class="label-content" v-if="!loading">
				<view class="label-content__item" v-for="(item,index) in list" :key="item.id" @click="add(index)">
					{{item.name}}
				</view>
			</view>
			<view class="no-data" v-if="list.length === 0 && !loading">
				当前没有数据
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				is_edit:false,
				labelList:[],
				list:[],
				loading:true
			}
		},
		onLoad() {
			this.getLabel()
		},
		methods: {
		
			editLabel(){
				// this.is_edit = !this.is_edit
				if(this.is_edit) {
					this.is_edit = false
					this.setUpdateLabel(this.labelList)
				}else {
				   this.is_edit = true	
				}
			},
			getLabel(){
				this.loading = true
				this.$api.get_label({
					type:'all'
				}).then(res => {
					console.log('res', res)
					const {data} = res
					this.labelList = data.filter(item => item.current)
					this.list = data.filter(item => !item.current)
				}).finally(() => {
					this.loading = false
				})
			},
			add(index) {
				if(!this.is_edit) return
				const item = this.list.splice(index,1);
				// console.log(item)
				if(item) {
					this.labelList.push(...item)
				}
			},
			del(index) {
				const item = this.labelList.splice(index,1);
				// console.log(item)
				if(item) {
					this.list.push(...item)
				}
			},
			setUpdateLabel(list){
				let newIds = []
				list.forEach(item => {
					newIds.push(item._id)
				})
				uni.showLoading()
				this.$api.update_label({
					label:newIds
				}).then(res => {
					uni.showToast({
						title:'标签已更新',
						icon:'none'
					})
					uni.$emit('labelChange')
				}).finally(() => {
					uni.hideLoading()
				})
			}
		}
	}
</script>

<style lang="scss">
	page{
		background-color: #f5f5f5;
	}
	.label{
		.label-box{
			background-color: #fff;
			margin-bottom: 10px;
			.label-header{
				display: flex;
				justify-content: space-between;
				border-bottom: 1px solid #f5f5f5;
				font-size: 14px;
				padding: 10px 15px;
				color: #666;
				.label-edit{
					color: #30b33a;
					font-weight: bold;
				}
			}
			.label-content{
				display: flex;
				flex-wrap: wrap;
				padding: 15px;
				padding-top: 0;
				.label-content__item{
					position: relative;
					padding: 2px 5px;
					border: 1px #666 solid;
					margin-top: 12px;
					margin-right: 10px;
					border-radius: 5px;
					color: #666;
					font-size: 14px;
					.icons-close{
						position: absolute;
						top: -8px;
						right: -8px;
						z-index: 99;
						background-color: #fff;
						border-radius: 50%;
					}
				}
		    }
		}
	}
	
	.no-data {
		width: 100%;
		color: #999;
		font-size: 14px;
		padding: 50px 0;
		text-align: center;
	}

</style>
